package sunwin.yoms.service.system;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsLogSubregion;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.dao.extend.AdminOperateDao;
import sunwin.yoms.domain.comm.AdminOperateDomain;

import java.util.Collection;
import java.util.Date;

/**
 * Created by watson on 2015/8/29.
 */
@Service
public class LogService {
    @Autowired
    private AdminOperateDao adminOperateDao;

    /**
     * 根据指定分区获取日志
     */
    public Collection<AdminOperateDomain> getPageLogs(AdminOperateDomain domain,
                                                      YomsPage yomsPage,
                                                      int subregionIndex,
                                                      @Param("startTime") Date startTime,
                                                      @Param("endTime") Date endTime) throws Exception {

        YomsLogSubregion subregion = adminOperateDao.getLogSubregion();
        subregion.setSubregionIndex(subregionIndex);
        Collection<AdminOperateDomain> adminOperateDomains = adminOperateDao.getLogs(domain, yomsPage, subregion, startTime, endTime);
        if(adminOperateDomains.size() == 0){
            throw YomsException.getInstance("没有找到日志记录", 1);
        }
        return adminOperateDomains;
    }

    /**
     * 获取默认分区
     * @return
     * @throws Exception
     */
    public YomsLogSubregion getDefaultSubregion() throws Exception {
        YomsLogSubregion subregion = adminOperateDao.getLogSubregion();
        return subregion;
    }

    /**
     * 将管理员操作记录到数据库
     * @param domain
     * @return 分区名
     * @throws YomsException
     */
    public String addLog(AdminOperateDomain domain) throws YomsException {
        return adminOperateDao.addLog(domain);
    }

    /**
     * 更新日志
     * @param domain
     * @param subregionName
     * @return
     * @throws YomsException
     */
    public void updateLog(AdminOperateDomain domain, String subregionName) throws YomsException {
        adminOperateDao.updateLog(domain, subregionName);
    }
}
